package com.game.spacetrader.map;

import java.io.Serializable;
import java.util.ArrayList;

import com.game.spacetrader.Game;
import com.game.spacetrader.Planet;
import com.game.spacetrader.PlanetView;
import com.game.spacetrader.R;
import com.game.spacetrader.TradeGoods.TradeGoodsView;
import com.game.spacetrader.data.SaveGame;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;

/**
 * Represents a view that shows all the planets (a map)
 *
 */
public class MapView extends Activity implements OnTouchListener, OnClickListener, Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static volatile MapView instance = null;
	private GameCanvas mGameCanvas = null;
	private TextView curLocation;
	private ArrayList<Planet> planets;
	double ratioX, ratioY;
	
	/**
	 * Creates the layout and the view
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.map);
        mGameCanvas = (GameCanvas) findViewById(R.id.canvas);
        mGameCanvas.setOnTouchListener(this);
        this.planets = mGameCanvas.getPlanets();
        this.ratioX = mGameCanvas.getRatioX();
        this.ratioY = mGameCanvas.getRatioY();
        curLocation = (TextView)findViewById(R.id.currLoc);
        setCurLoc();
        instance = this;
    }
    
    /**
	 * creates this as a singleton
	 * @return instance of MapView
	 */
	public static MapView getInstance(){
		if (instance == null) {
			synchronized (Game.class){
				if (instance == null) {
					instance = new MapView();
				}
			}
		}
		return instance;
	}

    /**
     * When screen is touched and finger moves
     * 
     */
	public boolean onTouch(View v, MotionEvent event) {
		float rawX = event.getX();
		float rawY = event.getY();
		double tempX, tempY;
		for(Planet planet:planets){
			tempX = Math.abs(rawX - planet.getX()*this.ratioX);
			tempY = Math.abs(rawY - planet.getY()*this.ratioY);
			//Log.d("MapScreen.onTouch", tempX + " " + tempY);
			if(tempX < 40 && tempY <40){
				Game.getInstance().setTravelTo(planet);
				goToPlanetView();
				return true;
			}
		}
		return false;
	}
	
	public void setCurLoc(){
		Coordinates c = Game.getInstance().getPlayer().getLocation();
		curLocation.setText("Current Location: ("+c.getX()+", "+c.getY()+")");
	}
	
	/**
	 * Goes to specific planet
	 */
	public void goToPlanetView(){
		Intent i = new Intent(this, PlanetView.class);
		startActivity(i);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Creates option menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_space_trader, menu);
	    return true;
	}
	
	/**
     * Listener for the options menu
     * @param item the item in the Menu that is clicked
     */
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.travel:
	        	Intent i = new Intent(this, MapView.class);
	        	startActivity(i);
	            return true;
	        case R.id.market:
	        	Intent i2 = new Intent(this, TradeGoodsView.class); startActivity(i2);
	        	return true;
	        case R.id.save:
	        	SaveGame saveGame = new SaveGame();
	        	saveGame.save((Context) this);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
}